package com.yonyou.crm.bpub.refervalue.appctrl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.login.context.LoginContextConstant;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.common.rest.constant.AppCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.bpub.refervalue.rmitf.IRefervalueRmService;
import com.yonyou.iuap.context.InvocationInfoProxy;


@RestController
@RequestMapping(value = "/{version}/mbpub/refervalues")
public class AppRefervalueCtrl {

	@Autowired
	IRefervalueRmService service;

	@RequestMapping(method = RequestMethod.GET)
	public Object getRefervalues(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		Map<String, Object> paraMap = new HashMap<String, Object>();
		
		String funcode = "";
		if (paramObj.containsKey("funcode")) {
			funcode = paramObj.getString("funcode");
		}
		paraMap.put("funcode", funcode);
		String refertype = paramObj.getString("refertype");
		paraMap.put("refertype", refertype);
		if (funcode.equals("") || refertype.equals("")) {
			throw new CrmMessageException(AppCodeConstant.ClientError, "");
		}
		Object condition = paramObj.get("condition");
		if (condition != null) {
			String conditionStr = String.valueOf(condition);
			paraMap.put("condition", conditionStr);
		}
		
		Object page = paramObj.get("page");
		Object pageSize = paramObj.get("pageSize");
		Page pageInfo= new Page();
		if (page != null && pageSize != null) {
			int pageNum = Integer.valueOf(String.valueOf(page));
			int pageSizeNum = Integer.valueOf(String.valueOf(pageSize));
			pageInfo= new Page(pageNum, pageSizeNum);
		}

		//TODO filter
		List<Map<String, Object>> filterList = (List<Map<String, Object>>)paramObj.get("filterlist");
		if (filterList != null && filterList.size() > 0) {
			int filterSize = filterList.size();
			for(int i = 0 ; i < filterSize; i++){
				Map<String, Object> filterMap = (Map<String, Object>) filterList.get(i);
				String key = String.valueOf(filterMap.get("key"));
				List<String> list = new ArrayList<String>();
				if (key == null || key.equals("")) {
					continue;
				} else if (key.startsWith("__Login__.")) {
					//TODO 需要确保key与登陆信息里的key保持一致
					key = key.substring("__Login__.".length());
					String[] loginContextArr = {"orgId", "deptId", "roleId"};
					if (Arrays.asList(loginContextArr).contains(key)) {
						paraMap.put(key, InvocationInfoProxy.getParameter(key.toLowerCase()));
					} else if ("userId".equals(key)) {
						paraMap.put(key, LoginContextConstant.UserId);
					} else {
						//nothing
					}
					continue;
				} else {
					List<Map<String, String>> dataList = (List<Map<String, String>>) filterMap.get("datalist");
					int dataListSize = dataList.size();
					for(int j = 0; j < dataListSize; j++){
						Map<String, String> data = dataList.get(j);
						list.add(data.get("id"));
					}
				}
				paraMap.put(key, list);
			}
		}

		Map<String, Object> map = service.getRefervalues(pageInfo, paraMap);
		return ResponseUtil.getObject(map);
	}
}
